package com.zcoj.wx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcoj.wx.mapper.WxUserMapper;
import com.zcoj.wx.pojo.WxUser;
import com.zcoj.wx.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/8/19 14:55
 */
@Service
public class WxUserServiceImpl implements WxUserService {

	@Autowired
	private WxUserMapper wxUserMapper;
	@Override
	//返回uid
	public Integer checkWxOpenId(String openId) {
		QueryWrapper<WxUser> wxUserQueryWrapper=new QueryWrapper<>();
		wxUserQueryWrapper.eq("open_id",openId);
		WxUser wxUser = wxUserMapper.selectOne(wxUserQueryWrapper);
		return Objects.nonNull(wxUser)?wxUser.getUid():0;
	}

	@Override
	public Integer insertWxUser(WxUser wxUser) {
		return wxUserMapper.insert(wxUser);
	}

	@Override
	public Integer findWxUserIDByOpenId(String openId) {
		QueryWrapper<WxUser> wxUserQueryWrapper=new QueryWrapper<>();
		wxUserQueryWrapper.eq("open_id",openId);
		wxUserQueryWrapper.select("id");
		WxUser wxUser = wxUserMapper.selectOne(wxUserQueryWrapper);
		return Objects.isNull(wxUser)?0:wxUser.getId();
	}

	@Override
	public WxUser findWxUserByUid(Integer uid) {
		QueryWrapper<WxUser> wxUserQueryWrapper=new QueryWrapper<>();
		wxUserQueryWrapper.eq("uid",uid);
		return wxUserMapper.selectOne(wxUserQueryWrapper);
	}

	@Override
	public Integer deleteWxUserById(Integer id) {
		return wxUserMapper.deleteById(id);
	}
}
